﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace T.FAS.Runtime.Metadata.Entity
{
    /// <summary>
    /// 元数据基类
    /// </summary>
    [Serializable]
    public class TMetadata : IMetaData, INotifyPropertyChanged, ICloneable
    {
        private TMetaDataCollection dependentObjects = null;

        public TMetadata()
        {
            dependentObjects = new TMetaDataCollection();
        }

        #region IMetadata属性实现
              
        /// <summary>
        /// ID
        /// </summary>
        public virtual string ID { get; set; }

        /// <summary>
        /// 编号
        /// </summary>
        public virtual string Code { get; set; }

        /// <summary>
        /// Name
        /// </summary>
        public virtual string Name { get; set; }

        /// <summary>
        ///元数据类型
        /// </summary>
        public virtual string MetaTypeID { get; set; }

        /// <summary>
        /// 元数据类型名称
        /// </summary>
        public virtual string MetaTypeName { get; set; }

        /// <summary>
        /// 隶属的命名空间
        /// </summary>
        public virtual string MetaSpaceID { get; set; }

        /// <summary>
        /// 版本
        /// </summary>
        public virtual string CurrentVersionID { get; set; }

        /// <summary>
        /// 当前版本
        /// </summary>
        public virtual string CurrentVersion { get; set; }

        /// <summary>
        /// 签入签出状态
        /// </summary>  
        public virtual MetaDataStatus MetaDataStatus { get; set; } = MetaDataStatus.New;

        /// <summary>
        /// 签出用户
        /// </summary>
        public virtual string CheckOutUser { get; set; }

        /// <summary>
        /// 深度
        /// </summary>
        public virtual int Depth { get; set; }

        /// <summary>
        /// 是否删除
        /// </summary>
        public virtual bool IsDeleted { get; set; }

        /// <summary>
        /// 元数据对象内容ID
        /// </summary>
        public virtual string ContentID { get; set; }

        /// <summary>
        /// 负责人
        /// </summary>
        public virtual string Owner { get; set; }

        /// <summary>
        /// 是否支持多语言
        /// </summary>
        public virtual bool IsMultiLanguageSupport { get; set; }

        /// <summary>
        /// 当前语言设置
        /// </summary>
        public virtual string Culture { get; set; }

        /// <summary>
        /// 扩展属性1
        /// </summary>
        public virtual string Ext1 { get; set; }

        /// <summary>
        /// 扩展属性2
        /// </summary>
        public virtual string Ext2 { get; set; }

        /// <summary>
        /// 扩展属性3
        /// </summary>
        public virtual string Ext3 { get; set; }

        /// <summary>
        /// 扩展属性集合
        /// </summary>
        public virtual List<MetaDataExtProperty> Ext { get; set; } = new List<MetaDataExtProperty>();

        /// <summary>
        /// 元数据管理信息
        /// </summary>
        public virtual List<MetaDataManageInfo> MetaDataManageInfos { get; set; } = new List<MetaDataManageInfo>();

        public virtual string TenantId { get; set; }

        /// <summary>
        /// 对应开发层次编号
        /// </summary>
        public virtual string DevLevelCode { get; set; }

        /// <summary>
        /// 元数据来源类型
        /// </summary>
        public virtual MetaDataSourceType SourceType { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public virtual string Creator { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public virtual DateTime CreateTime { get; set; }

        /// <summary>
        /// 最后修改人
        /// </summary>
        public virtual string LastModifier { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        public virtual DateTime LastModifyTime { get; set; }

        /// <summary>
        /// 项目分支标识
        /// </summary>
        public string ProjectBranchID { get; set; }

        public RefSchema RefSchema { get; set; }

        public IRefSchemaEntity CurrSchemaEnitty { get; set; }

        public List<Term> TermList { get; set; }

        public MetaDataCheckOut CheckOutMetaData { get; set; }
        #endregion

        #region INotifyPropertyChanged接口实现

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected virtual void Metadata_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            NotifyPropertyChanged(e.PropertyName);
        }

        #endregion

        /// <summary>
        /// 创建作为当前实例副本的新对象。
        /// </summary>
        /// <returns>作为此实例副本的新对象。</returns>
        public object Clone()
        {
            var newObj = this.MemberwiseClone() as TMetadata;
            if (this.Ext.Count > 0)
            {
                newObj.Ext = new List<MetaDataExtProperty>();
                foreach (var item in Ext)
                {
                    newObj.Ext.Add(item.Clone() as MetaDataExtProperty);
                }
            }
            if (this.MetaDataManageInfos != null)
            {
                newObj.MetaDataManageInfos = new List<MetaDataManageInfo>();
                foreach (var item in this.MetaDataManageInfos.ToArray())
                {
                    newObj.MetaDataManageInfos.Add(item.Clone() as MetaDataManageInfo);
                }
            }
            if (this.Content != null)
            {
                newObj.Content = this.Content.Clone() as IMetaDataContent;
            }

            return newObj;
        }

        /// <summary>
        /// 依赖的元数据列表
        /// </summary>
        public TMetaDataCollection DependentMetaDatas
        {
            get { return dependentObjects; }
        }
        
        public List<MetaDataDependencyVersion> MetaDataDependencies { get; set; }

        private IMetaDataContent content; 

        /// <summary>
        /// 元数据内容
        /// </summary>             
        public IMetaDataContent Content
        {
            get { return content; }
            set
            {
                this.content = value;
                if (value != null)
                    this.content.PropertyChanged += Metadata_PropertyChanged;

                if ((string.IsNullOrEmpty(this.ID)) && value != null && (value.MetaData != null) && (string.IsNullOrEmpty(value.MetaData.ID) == false))
                    this.PropertyCopy(value.MetaData);
            }
        }

        public string DataID { get; set; }

        public string ContentJSON { get; set; }

        public string MetaDataJson { get; set; }

        /// <summary>
        /// 属性拷贝
        /// </summary>
        /// <param name="srcdata"></param>
        private void PropertyCopy(IMetaData srcdata)
        {
            this.ID = srcdata.ID;
            this.Code = srcdata.Code;
            this.CreateTime = srcdata.CreateTime;
            this.Culture = srcdata.Culture;
            this.CheckOutUser = srcdata.CheckOutUser;
            this.ContentID = srcdata.ContentID;
            //this.Ext = srcdata.Ext;
            this.DevLevelCode = srcdata.DevLevelCode;
            this.MetaTypeID = srcdata.MetaTypeID;
            this.MetaTypeName = srcdata.MetaTypeName;
            this.Ext1 = srcdata.Ext1;
            this.Ext2 = srcdata.Ext2;
            this.Ext3 = srcdata.Ext3;          
            this.Creator = srcdata.Creator;
            this.CreateTime = srcdata.CreateTime;
            this.LastModifier = srcdata.LastModifier;
            this.LastModifyTime = srcdata.LastModifyTime;
            this.Name = srcdata.Name;
            this.IsMultiLanguageSupport = srcdata.IsMultiLanguageSupport;
            this.IsDeleted = srcdata.IsDeleted;
            //this.GUID = srcdata.GUID;
            this.CurrentVersion = srcdata.CurrentVersion;
            this.CurrentVersionID = srcdata.CurrentVersionID;
            this.SourceType = srcdata.SourceType;
            //this.Depth = srcdata.Depth;            
            this.MetaDataStatus = srcdata.MetaDataStatus;
            this.MetaSpaceID = srcdata.MetaSpaceID;
            this.Name = srcdata.Name;
            this.Owner = srcdata.Owner;
            this.TenantId = srcdata.TenantId;
            this.ProjectBranchID = srcdata.ProjectBranchID;
        }
    }
}
